package com.zzyk.web.controller;

import com.zzyk.main.configuration.aspectj.annotation.OperaLog;
import com.zzyk.main.configuration.exception.DeviceException;
import com.zzyk.main.configuration.exception.UserException;
import com.zzyk.main.controller.BaseController;
import com.zzyk.main.model.pojo.DeviceType;
import com.zzyk.main.model.pojo.SysUser;
import com.zzyk.main.model.vo.Message;
import com.zzyk.main.service.DeviceTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author wss_PC
 * @DESCRIPTION 设备类型
 * @create 2020/10/26 14:26
 */
@RequestMapping("/deviceType")
@RestController
public class DeviceTypeController extends BaseController {
    @Autowired
    private DeviceTypeService deviceTypeService;

    @GetMapping("/getNoTypeDevice")
    public Message getNoTypeDevice() throws UserException {
        SysUser user = currentUser();
        return Message.success().addData("data",deviceTypeService.getNoTypeDevice(user.getArea()));
    }
    @PostMapping("/uniqueName")
    public  Map<String, Boolean> uniqueName(Integer methodType,String title,Integer id){
    return deviceTypeService.uniqueName(methodType,title,id);
    }
    @GetMapping("/selectAllByRank")
    public Message selectAllByRank(Integer rank){
        return Message.success().addData("data",deviceTypeService.selectByRank(rank));
    }
    @GetMapping("/selectAllByParentId")
    public Message selectAllByParentId( @RequestParam(value = "parentId", defaultValue = "0") Integer parentId){
        return Message.success().addData("data",deviceTypeService.selectByParentId(parentId));
    }

    /**
     * 根据设备类型ID修改设备类型信息
     * @param deviceType
     * @return
     */
    @OperaLog("修改设备类型")
    @PostMapping("/updateDeviceTypeById")
    public Message updateDeviceTypeById(DeviceType deviceType) throws DeviceException {
            if (deviceTypeService.updateByPrimaryKeySelective(deviceType) > 0) {
                return Message.success();
            } else {
                return Message.failed("修改失败");
            }
    }
    /**
     * 查询所有设备类型数据
     * @return
     */

    @GetMapping("/selectDeviceTypeTree")
    public  Message selectAllDeviceType(){
        return Message.success().addData("data",deviceTypeService.selectDeviceTypeTree());
    }
    /**
     * 添加设备类型
     *
     * @return
     */
    @OperaLog("添加设备类型")
    @PostMapping("insertDeviceType")
    public Message insertDeviceType(DeviceType deviceType) {
        try {
            if (deviceTypeService.insertSelective(deviceType) > 0) {
                return Message.success();
            } else {
                return Message.failed("添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Message.failed("添加失败");
        }
    }
}